package com.omuo.utils.collection.demo.simple;

import com.omuo.utils.collection.facade.Mapping;
import com.omuo.utils.collection.facade.StandardMap2BeanWithConvertProcessorImpl;
import com.omuo.utils.collection.facade.StandardMappingImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 示例测试含转换
 *
 * @author omuao
 */
public class DemoConvertTest {

    public static void main(String[] args) {
        List<Mapping<Object, String>> mappings = new ArrayList<>();

        mappings.add(new StandardMappingImpl(0, "name"));

        mappings.add(new StandardMappingImpl("sex", "sex"));

        mappings.add(new StandardMappingImpl("code", "code"));

        Map<Object, Object> map = new HashMap<>(20);

        map.put(0, "张三");

        map.put("sex", "1");

        map.put("code", "Test");

        StandardMap2BeanWithConvertProcessorImpl standardMap2BeanProcessor = new StandardMap2BeanWithConvertProcessorImpl();
        standardMap2BeanProcessor.registerConverter("sex", (source, target, sourceValue) -> Integer.valueOf(sourceValue.toString()));
        standardMap2BeanProcessor.registerConverter("code", (source, target, sourceValue) -> sourceValue.equals("Test") ? "" : sourceValue);
        standardMap2BeanProcessor.setMappings(mappings);
        standardMap2BeanProcessor.setOutBeanClass(Demo2Bean.class);
        standardMap2BeanProcessor.setInObjectMap(map);
        standardMap2BeanProcessor.complete();
        System.out.println(standardMap2BeanProcessor.getOutBean());
    }
}
